package com.flyco.modules.ums.service.impl;

import com.flyco.modules.ums.entity.UmsMemberLevel;
import com.flyco.modules.ums.entity.UmsMemberLevelRights;
import com.flyco.modules.ums.entity.UmsMemberLevelBanner;
import com.flyco.modules.ums.mapper.UmsMemberLevelRightsMapper;
import com.flyco.modules.ums.mapper.UmsMemberLevelBannerMapper;
import com.flyco.modules.ums.mapper.UmsMemberLevelMapper;
import com.flyco.modules.ums.service.IUmsMemberLevelService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 会员等级表
 * @Author: flyco
 * @Date:   2022-09-29
 * @Version: V1.0
 */
@Service
public class UmsMemberLevelServiceImpl extends ServiceImpl<UmsMemberLevelMapper, UmsMemberLevel> implements IUmsMemberLevelService {

	@Autowired
	private UmsMemberLevelMapper umsMemberLevelMapper;
	@Autowired
	private UmsMemberLevelRightsMapper umsMemberLevelRightsMapper;
	@Autowired
	private UmsMemberLevelBannerMapper umsMemberLevelBannerMapper;
	
	@Override
	@Transactional
	public void saveMain(UmsMemberLevel umsMemberLevel, List<UmsMemberLevelRights> umsMemberLevelRightsList,List<UmsMemberLevelBanner> umsMemberLevelBannerList) {
		umsMemberLevelMapper.insert(umsMemberLevel);
		for(UmsMemberLevelRights entity:umsMemberLevelRightsList) {
			//外键设置
			entity.setLevelId(umsMemberLevel.getId());
			umsMemberLevelRightsMapper.insert(entity);
		}
		for(UmsMemberLevelBanner entity:umsMemberLevelBannerList) {
			//外键设置
			entity.setLevelId(umsMemberLevel.getId());
			umsMemberLevelBannerMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(UmsMemberLevel umsMemberLevel,List<UmsMemberLevelRights> umsMemberLevelRightsList,List<UmsMemberLevelBanner> umsMemberLevelBannerList) {
		umsMemberLevelMapper.updateById(umsMemberLevel);
		
		//1.先删除子表数据
		umsMemberLevelRightsMapper.deleteByMainId(umsMemberLevel.getId());
		umsMemberLevelBannerMapper.deleteByMainId(umsMemberLevel.getId());
		
		//2.子表数据重新插入
		for(UmsMemberLevelRights entity:umsMemberLevelRightsList) {
			//外键设置
			entity.setLevelId(umsMemberLevel.getId());
			umsMemberLevelRightsMapper.insert(entity);
		}
		for(UmsMemberLevelBanner entity:umsMemberLevelBannerList) {
			//外键设置
			entity.setLevelId(umsMemberLevel.getId());
			umsMemberLevelBannerMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		umsMemberLevelRightsMapper.deleteByMainId(id);
		umsMemberLevelBannerMapper.deleteByMainId(id);
		umsMemberLevelMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			umsMemberLevelRightsMapper.deleteByMainId(Long.valueOf(id.toString()));
			umsMemberLevelBannerMapper.deleteByMainId(Long.valueOf(id.toString()));
			umsMemberLevelMapper.deleteById(id);
		}
	}
	
}
